<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\base\Exception;
use common\models\Product;

class ProductDataForm extends Model
{
	public $name;
	public $price_begin;
	public $price_end;
	public $time;

	public function formName()
    {
        return '';
    }

	public function rules()
	{
		return [
			[['name', 'price_begin', 'price_end', 'time'], 'safe']
		];
	}

	public function query()
	{
		$data = Product::find()->where('id <> 0');
		if ($this->name) {
			$data = $data->andWhere(['name' => $this->name]);
		} 
		if ($this->price_begin && !$this->price_end) {
			$data = $data->andWhere('price >= ' . $this->price_begin);
		}
		if ($this->price_end && !$this->price_begin) {
			$data = $data->andWhere('price <= ' . $this->price_end);
		}
		if ($this->price_begin && $this->price_end) {
			$data = $data->andWhere('price >= ' . $this->price_begin)->andWhere('price <= ' . $this->price_end);
		}
		if ($this->time) {
			$re = explode('-', $this->time);
			$begin = strtotime(trim($re[0]));
			$end = strtotime(trim($re[1]));
			$data = $data->andWhere('created_at >= ' . $begin)->andWhere('created_at <= ' . $end);
		}

		return $data;
	}

}	